package mrusanov.player

import java.util.Date

object PhysicalShape {
  val MAX_PHYSICAL_SHAPE = BigDecimal(100)
  val MIN_PHYSICAL_SHAPE = BigDecimal(0)

  private[player] val RELAX_ONE_DAY_INCREMENT = 3
  private[player] val TIRED_AFTER_BATTLE_DECREMENT = 20
}

trait PhysicalShape {
  self:Player =>
  private var _physicalShape = PhysicalShape.MAX_PHYSICAL_SHAPE

  private var _injury:Option[Injury] = None

  def injury = _injury
  def injury_=(value:Option[Injury]) {
    require(value.isDefined, "Should not be possible to set empty injury")
    require(_injury.isEmpty, "Player already has injury")

    this._injury = value
    _physicalShape -= value.get.physicalFormLossAfterInjury
    normalizePhysicalShape()
  }

  def physicalShape = _physicalShape

  def relaxOneDay(date:Date) {
    injury match {
      case None => {
        _physicalShape += PhysicalShape.RELAX_ONE_DAY_INCREMENT
        normalizePhysicalShape()
      }
      case Some(inj) => {
        if (inj.isOver(date)) {
          _injury = None
        }
      }
    }
  }


  def tiredAfterBattle() {
    require(!isInjured, "Injured player cann't take part in battle")
    _physicalShape -= PhysicalShape.TIRED_AFTER_BATTLE_DECREMENT
  }

  private def normalizePhysicalShape() {
    _physicalShape = PhysicalShape.MAX_PHYSICAL_SHAPE.min(_physicalShape)
    _physicalShape = PhysicalShape.MIN_PHYSICAL_SHAPE.max(_physicalShape)
  }

  def isInjured = injury.isDefined

}
